package com.restaurant.ressys.controller;


import com.restaurant.ressys.entity.Category;
import com.restaurant.ressys.entity.Food;
import com.restaurant.ressys.service.CategoryService;
import com.restaurant.ressys.service.FoodService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangsan
 * @since 2022-02-28
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;
    @Resource
    private FoodService fs;
    @GetMapping("/getAll")
    public List<Category> categoryList(){
        List<Food> list = fs.list();
        List<Category> cateList = categoryService.list();
        return cateList.stream().peek((c)->{
            long count = list.stream().filter((f) -> {
                return f.getCid() == c.getId();
            }).count();
            c.setCount((int)count);
        }).collect(Collectors.toList());
    }
    @PostMapping("/add")
    public void add(@RequestBody Category category, HttpSession session){
        categoryService.save(category);
    }
    @DeleteMapping("/del/{id}")
    public void delete(@PathVariable("id") int id){
        categoryService.removeById(id);
    }



}

